package linter.linterjava.controller.note;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.Note;
import linter.linterjava.POJO.DTO.NoteDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.POJO.UtilDTO.TypeDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.NoteService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@ApiOperation("笔记基础模块")
@RestController
@RequestMapping("note")
public class BaseNoteController {

    @Resource
    UserService userService;
    @Resource
    NoteService noteService;
    private final UserQueryService userQueryService;
    @Autowired
    public BaseNoteController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }


    @PostMapping("createNote")
    @ApiOperation("创建笔记")
    public ResultVO<String> createNote(@RequestBody NoteDTO noteDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.creatNote(noteDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("updateNoteByNoteId")
    @ApiOperation("修改笔记")
    public ResultVO<String> updateNoteByNoteId(@RequestBody NoteDTO noteDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.updateNoteByNoteId(noteDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("deleteNoteByNoteId")
    @ApiOperation("删除笔记")
    public ResultVO<String> deleteNoteByNoteId(@RequestParam Long noteId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.deleteNoteByNoteId(noteId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param pageInfoDTO PageInfo工具类 -- pageSize、pageNum
     */
    @PostMapping("queryNote")
    @ApiOperation("查询笔记 -- 所有未分组")
    public ResultVO<PageInfo<Note>> queryNote(@RequestBody PageInfoDTO pageInfoDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.queryNote(pageInfoDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param pageInfoDTO PageInfo工具类 -- pageSize、pageNum
     */
    @PostMapping("queryALLNote")
    @ApiOperation("查询笔记 -- 所有")
    public ResultVO<PageInfo<Note>> queryAllNote(@RequestBody PageInfoDTO pageInfoDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.queryAllNote(uuid, pageInfoDTO)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param typeDTO 接收泛型
     */
    @PostMapping("queryNoteByNoteAbout")
    @ApiOperation("查询笔记 -- 根据笔记相关信息")
    public ResultVO<?> queryNoteByNoteAbout(@RequestBody TypeDTO<?> typeDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.queryNoteByNoteAbout(typeDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("viewNote")
    @ApiOperation("查看笔记")
    public ResultVO<Note> queryNote(@RequestParam Long noteId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) { return ResultVO.success(noteService.viewNote(noteId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }




}
